﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using tracker.Models.DTO;
using System.Data;

namespace tracker.Models.DAL
{
    public class DeviceDAL : ErpCatalogueBaseBL
    {
        #region Properties Define
        private string FIELD_USERNAME = "Username";
        private string FIELD_SECURITYCODE = "SecurityCode";
        private string FIELD_DEVICEKEYSECURITY = "DeviceKeySecurity";
        private string FIELD_DEVICEKEY = "DeviceKey";
        private string FIELD_SIMNUMBER ="SimNumber";
        
        #endregion

        #region STORE NAME
        public const string DEVICE_SELECTBYUSERNAME = "Device_SelectByUserName";
        public const string DEVICE_SELECTBYSECURITYCODE = "Device_SelectBySecurityCode";
        public const string DEVICE_SELECTBYDEVICEKEYSECURITY = "Device_SelectByDeviceKeySecurity";
        public const string DEVICE_SELECTBYDEVICEKEY = "Device_SelectByDeviceKey";
        public const string DEVICE_INSERT = "Device_Insert";
        public const string DEVICE_UPDATE = "Device_Update";
        public const string DEVICE_CHANGEUSER = "Device_ChangeUser";
        public const string DEVICE_REMOVE = "Device_Remove";
        public const string FIELD_PASSWORD ="Password";
        #endregion

        public List<EDevice> getByUserName(string userName)
        {
            List<EDevice> lstDevice = null;
            DataSet ds = ErpCatalogueBaseBL.SelectAllWithParameter(DEVICE_SELECTBYUSERNAME,
                                                                 new string[] { FIELD_USERNAME },
                                                                 new object[] { userName });
            
            if (Utilities.hasRow(ds))
            {
                lstDevice = new List<EDevice>();
                DataTable dt = ds.Tables[0];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    EDevice dv = new EDevice();
                    dv = EDevice.createDevice(dt.Rows[i]);
                    lstDevice.Add(dv);
                }
            }
            return lstDevice;
        }
        public List<EDevice> getBySecurityCode(string SecurityCode)
        {
            List<EDevice> lstDevice = new List<EDevice>();
            DataSet ds = ErpCatalogueBaseBL.SelectAllWithParameter(DEVICE_SELECTBYSECURITYCODE,
                                                                 new string[] { FIELD_SECURITYCODE },
                                                                 new object[] { SecurityCode });

            if (Utilities.hasRow(ds))
            {
                lstDevice = new List<EDevice>();
                DataTable dt = ds.Tables[0];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    EDevice dv = new EDevice();
                    dv = EDevice.createDevice(dt.Rows[i]);
                    lstDevice.Add(dv);
                }
            }
            return lstDevice;
        }
        public EDevice getByDeviceKeySecurity(string key)
        {
            List<EDevice> lstDevice = new List<EDevice>();
            DataSet ds = ErpCatalogueBaseBL.SelectAllWithParameter(DEVICE_SELECTBYDEVICEKEYSECURITY,
                                                                 new string[] { FIELD_DEVICEKEYSECURITY },
                                                                 new object[] { key });
            EDevice dv = null;
            
            if (Utilities.hasRow(ds))
            {
                DataTable dt = ds.Tables[0];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    new EDevice();
                    dv = EDevice.createDevice(dt.Rows[i]);
                    lstDevice.Add(dv);
                }
            }
            return dv;
        }
        public EDevice getByDeviceKey(string key)
        {
            
            DataSet ds = ErpCatalogueBaseBL.SelectAllWithParameter(DEVICE_SELECTBYDEVICEKEY,
                                                                 new string[] { FIELD_DEVICEKEY },
                                                                 new object[] { key });
            EDevice dv = null;

            if (Utilities.hasRow(ds))
            {
                DataTable dt = ds.Tables[0];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    dv = new EDevice();
                    dv = EDevice.createDevice(dt.Rows[i]);
                }
            }
            return dv;
        }
        public string insert(EDevice ODevice)
        {
            string[] addFieldName = new string[]{
                    "DeviceKey", 		
                    "DeviceTypeID",		
                    "DeviceName", 		
                    "CreateBy", 		
                    "Password", 		
                    "SimNumber",		
                    "PictureOfDevide", 	
                    "UserID", 			
                    "DeviceKeySecurity"
            };

            object[] data = new object[] { 
                    ODevice.DeviceKey		,
                    ODevice.DeviceTypeID	,
                    ODevice.DeviceName		,
                    ODevice.CreateBy		,
                    ODevice.Password       ,
                    ODevice.SimNumber		,
                    ODevice.PictureOfDevide,
                    ODevice.UserID			,
                    ODevice.DeviceKey
                 };
            return ErpCatalogueBaseBL.Insert(DEVICE_INSERT, addFieldName, data);
        }

        public string  update(EDevice dv)
        {
            string[] fieldNames = new string[] { "DeviceKeySecurity",
                                                "DeviceTypeID",
                                                "DeviceName",
                                                "ModifyBy",
                                                "ModifyDate", 
                                                "Password", 
                                                "SimNumber",
                                                "PictureOfDevide"
                                            };
            object[] data = new object[] { dv.DeviceKeySecurity,
                                            dv.DeviceTypeID,
                                            dv.DeviceName,
                                            dv.ModifyBy,
                                            DateTime.Now,
                                            dv.Password,
                                            dv.SimNumber,
                                            dv.PictureOfDevide
                                            };
            return ErpCatalogueBaseBL.Update(DEVICE_UPDATE, fieldNames, data);
        }

        // Update userid to userSecurityCode
        public string changeUser(string userSecurityCode, string deviceKey, string password, string simnumber)
        {
            string[] fieldNames = new string[] { 
                                                "SecurityCode",
                                                 "DeviceKey",
                                                "Password",                                                
                                                "SimNumber",
                                            };
            object[] data = new object[] { userSecurityCode,
                                            deviceKey,
                                            password,                                            
                                            simnumber
                                            };
            DataSet ds = ErpCatalogueBaseBL.SelectAllWithParameter(DEVICE_CHANGEUSER, fieldNames, data);

            if (Utilities.hasRow(ds))
            {
                if ((int)ds.Tables[0].Rows[0][0] !=0)
                {
                    return "0";
                }
            }
            return "-1";
        }
        public string removeDevice(string UserSecurityCode, string deviceKey, string password)
        {
            string[] fieldNames = new string[] { FIELD_SECURITYCODE,
                                                FIELD_DEVICEKEY,
                                                FIELD_PASSWORD
                                            };
            object[] data = new object[] { UserSecurityCode,
                deviceKey,
                password
                                            };
            return ErpCatalogueBaseBL.Update(DEVICE_REMOVE, fieldNames, data);
        }
    }
}